package com.muse.openfeign.controller;

import java.util.Arrays;
import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.muse.openfeign.dto.User;

import lombok.extern.slf4j.Slf4j;

/**
 * @description
 * @author: muse
 * @create: 2021-08-17 21:51
 **/
@Slf4j
@RequestMapping("/provider")
@RestController
public class ProducerController {
    // http://localhost:8000/provider/hello
    @GetMapping("/hello")
    public String hello() {
        log.info("provider hello");
        return "provider hello";
    }

    /**
     * 传递基本类型参数
     */
    @GetMapping("/baseParam")
    public String baseParam(@RequestParam("name") String name, @RequestParam("age") Integer age) {
        log.info("provider baseParam name={} age={}", name, age);
        return String.format("provider name=%s age=%s", name, age);
    }

    /**
     * 传递基本类型参数——路径
     */
    @GetMapping("/baseParamPath/{name}/{age}")
    public String baseParamPath(@PathVariable("name") String name, @PathVariable("age") Integer age) {
        log.info("provider baseParamPath name={} age={}", name, age);
        return String.format("provider name=%s age=%s", name, age);
    }

    /**
     * 传递对象类型参数
     */
    @PostMapping("/objectParam")
    public String objectParam(@RequestBody User user) {
        log.info("provider objectParam name={} age={}", user.getName(), user.getAge());
        return String.format("provider name=%s age=%s", user.getName(), user.getAge());
    }

    /**
     * 传递数组类型参数
     */
    @GetMapping("/arrayParam")
    public String arrayParam(@RequestParam("params") String[] params) {
        Arrays.stream(params).forEach(param -> log.info("param={}", param));
        return Arrays.toString(params);
    }

    /**
     * 传递集合类型参数
     * http://localhost:8000/provider/listParam?params=1&params=2
     */
    @GetMapping("/listParam")
    public String listParam(@RequestParam("params") List<String> params) {
        params.stream().forEach(param -> log.info("param={}", param));
        return params.toString();
    }

    /**
     * 返回对象类型结果
     * http://localhost:8000/provider/responseObj?userId=111
     */
    @GetMapping("/responseObj")
    public User responseObj(@RequestParam("userId") String userId) {
        log.info("consumer responseObj userId={}", userId);
        /** 模拟通过userId去数据库中查找到对应用户信息*/
        User user = new User("muse", 30);
        return user;
    }

    /**
     * 测试OpenFeign默认1秒请求超时
     * http://localhost:8000/producer/timeoutDemo
     */
    @GetMapping("/timeoutDemo")
    public String timeoutDemo() throws InterruptedException {
        log.info("producer timeoutDemo!");
        Thread.sleep(2000);
        return "producer timeoutDemo!";
    }

}
